package com.landscope.issue.utils;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.CacheObj;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用户缓存
 * @return {@link * @return null}
 * @throws
 * @author 瞿子朋
 * @date 2020/9/7 15:14
 * @version v1.0
 **/
public class UserLogoutCacheUtils extends CacheUtil {
    private static final TimedCache<String, Object> timedCache = CacheUtil.newTimedCache(DateUnit.DAY.getMillis() * 30);
    static {
        timedCache.schedulePrune(DateUnit.SECOND.getMillis() * 1);
    }

    public static void put(String key, Object value) {
        timedCache.put(key, value);
    }

    public static void put(String key, Object value,long timeout) {
        timedCache.put(key, value, timeout * DateUnit.MINUTE.getMillis());
    }

    public static Object get(String key) {
        return get(key, true);
    }

    public static Object get(String key, boolean prolong) {
        Object obj = timedCache.get(key);
        if(prolong) {
            put(key, obj);
        }
        return timedCache.get(key);
    }

    public static void remove(String key){
        timedCache.remove(key);
    }

    public static List<CacheObj<String, Object>> getAll() {
        List<CacheObj<String, Object>> datas = new ArrayList<>();
        Iterator<CacheObj<String, Object>> coi = timedCache.cacheObjIterator();
        while (coi.hasNext()) {
            CacheObj<String, Object> co = coi.next();
            datas.add(co);
        }
        return datas;
    }

    public static void clearAll() {
        timedCache.clear();
    }
}
